﻿using Lg.Core.Extensions;
using Newtonsoft.Json.Linq;
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;

namespace Lg.Core.Form.Types
{
    public class IdNumber : BaseType, IType
    {
        
        public string? IdType { get; set; }
        public void Verify()
        {
            if (this.IsRender)
            {
                if (Required)
                {
                    Validate.Required(Value, $"字段:{Title},不可为空！");
                }
                if (!string.IsNullOrEmpty(IdType) && IdType.Contains("身份证"))
                {
                    Validate.IdCard(Value!, $"字段：{Title}不是有效的身份证格式！");
                }
            }
        }
        public override FormData? SetValue(JObject formData)
        {
            string? value = formData[this.Name!]?.ToString();
            if (!string.IsNullOrEmpty(value))
            {
                var valueType = value.ToModel<ValueType>();
                if (valueType != null)
                {
                    this.Value = valueType.value;
                    this.IdType = valueType.type;
                }
                else
                {
                    this.Value="";
                    this.IdType = "身份证";
                }
            }
            else
            {
                this.Value = "";
                this.IdType = "身份证";
            }
            
            return this.GetValue();
        }

        public override string? FormatValue(string value)
        {
            if (!string.IsNullOrEmpty(value) && value != "lg-no-render")
            {
                var valueType = value.ToModel<ValueType>();
                if (valueType != null)
                {
                    return valueType.value;
                }
                else
                {
                    throw new Exception("内容格式不合法,无法解析");
                }
            }
            else
            {
               return "";
            }
        }
        public override List<FormData> FormatValue(List<FormData> list, JObject formData)
        {
            string? value = formData[this.Name!]?.ToString();
            var fd = new FormData { Name = Name, Value = "", Title = Title, ExportName = ExportName };
            if (!string.IsNullOrEmpty(value) && value != "lg-no-render")
            {
                var valueType = value.ToModel<ValueType>();
                if (valueType != null)
                {
                    fd.Value = valueType.value;
                }
            }
            list.Add(fd);
            return list;
        }
        private class ValueType
        {
            public string? type { get; set; }
            public string? value { get; set; }
        }


        public void GetSaveDbValue(Dictionary<string, dynamic> formData, string inputValue)
        {
            var s = "护照";
            if (!string.IsNullOrEmpty(inputValue)&&inputValue.Length==18) 
            {
                s = "身份证";
            }
            formData.Add(Name, new ValueType { type=s,value=inputValue});
        }
    }
}
